#include "KDM1Detector.hh"
#include "KDM1HitsCollection.hh"
#include "KDM1Hit.hh"
#include "G4RunManager.hh"
#include "G4SDManager.hh"
#include "KDM1Globals.hh"
#include <sstream>

KDM1Detector::KDM1Detector(G4String name):G4VSensitiveDetector(name)
{
	hitsCollection = 0;
	collectionName.insert("1");
}

KDM1Detector::~KDM1Detector()
{
	;
}

void KDM1Detector::Initialize(G4HCofThisEvent* hcc)
{
	hitsCollection = new KDM1HitsCollection(SensitiveDetectorName,collectionName[0]);

	G4SDManager* sdMan = G4SDManager::GetSDMpointer();

	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID,hitsCollection);
}

void KDM1Detector::EndOfEvent(G4HCofThisEvent*)
{
	;
}

G4bool KDM1Detector::ProcessHits(G4Step *step, G4TouchableHistory*)
{
	G4Track* track = step->GetTrack();
	double deposit = step->GetTotalEnergyDeposit();
	if(deposit == 0.0) return false;
	double time = track->GetGlobalTime();
	G4ThreeVector pos = track->GetPosition();

	KDM1Hit* hit = new KDM1Hit(time,deposit,pos);
	hitsCollection->Insert(hit);

	return true;
}
